"""
@author: 方杰
@file: 爬取官网图片.py
@time: 2023/6/4 14:13
"""
import requests
import re
import time
import os

# 官方网址
# url = input("请输入官网的地址:")
url = "https://www.sanchuangedu.cn"

# 先创建一个空目录，用于存放下载的图片
if not os.path.exists('scimg'):
    os.makedirs('scimg')

# 创建get_html获取网页的htm代码
def get_html(url):
    for i in range(3):      # 三次请求机会
        try:
            response = requests.get(url)  # 发起get请求
            if response.status_code == 200:  # 状态码为200则成功
                response.encoding = "utf-8"  # 返回的编码格式改为utf-8
                return response.text    # 返回htm代码
            else:
                time.sleep(1)
        except:
            time.sleep(1)

htm = get_html(url)
# print(htm)
img = re.findall(r"<img.*>", htm)  # 查找img标签(贪婪)
print(img)
for src in img:
    match = re.findall(r'src=(\S+)', src)  # 捕获分组
    # print(match[0])

    # 发送请求并下载图片
    get_img = requests.get(f"{url}/{match[0]}")
    if get_img.status_code == 200:
        get_img.encoding = "utf-8"
        with open(f"scimg/{match[0]}", "wb") as f:  # b表示以二进制模式打开文件
            f.write(get_img.content)  # 写入二进制数据
            print(f"地址为{url}/{match[0]}的图片已经下载成功")
